var
	vk_members_data = {},
	lastCommentsResponse,
	lastCommentsPage = null,
	baseURL = window.location.protocol + '//' + window.location.hostname + '/';

function array_unique(ar)
{
	if (ar.length && typeof ar !== 'string')
	{
		var sorter = {};
		var out = [];
		for (var i=0, j=ar.length; i<j; i++)
		{
			if(!sorter[ar[i]+typeof ar[i]])
			{
				out.push(ar[i]);
				sorter[ar[i]+typeof ar[i]]=true;
			}
		}
	}
	return out || ar;
}
	
function doLogin()
{
	VK.Auth.login
	(
		null,
		VK.access.FRIENDS | VK.access.WIKI
	);
}

function doLogout()
{
	VK.Auth.logout(logoutOpenAPI);
}

function loginOpenAPI()
{
	getInitData();
}

function logoutOpenAPI()
{
	/*window.location.reload();*/
	window.location = baseURL;
}

function getInitData()
{
	var code;
	code = 'return {';
		code += 'me: API.getProfiles({uids: API.getVariable({key: 1280}), fields: "photo"})[0]';
		code += ',info: API.getGroupsFull({gids:1})[0]';
		code += ',news: API.pages.get({gid:1, pid: 2424933, need_html: 1})';
		code += ',friends: API.friends.get({fields: "photo"})';
		//code += ',friends: API.getProfiles({uids: API.getAppFriends(), fields: "photo"})';
		code += '};';
	VK.Api.call('execute', {'code': code}, onGetInitData);
}

function onGetInitData(data)
{
	var r, i, j, html;
	if (data.response)
	{
		r = data.response;

    	/* Insert user info */
		if (r.me)
		{
			ge('openapi_user').innerHTML = r.me.first_name + ' ' + r.me.last_name;
			ge('openapi_userlink').href = '/id' + r.me.uid;
			ge('openapi_userphoto').src = r.me.photo;
		}
		
		/* Insert Group info */
		/*if (r.info)
		{
			ge('group_link').href = '/club' + r.info.gid;
			ge('logo_img').src = r.info.photo;
		}*/

		/* Insert news */
		/*if (r.news)
		{
			ge('news_title').innerHTML = r.news.title;
			ge('news').innerHTML = r.news.html;
		}*/

		/* Insert friends */
		html = '';
		for (i = 0, j = r.friends.length; i < j; i++)
		{
			
			if (i >= 12) break;
				html += '<div onmouseout="this.className=\'list_cell\';" onmouseover="this.className=\'list_cell_over\'" class="list_cell"><a href="/id'+r.friends[i]['uid']+'"><div class="list_border_wrap"><div class="list_wrap"><div class="list_div"><div class="list_image"><img width="50" src="'+r.friends[i]['photo']+'"></div></div><div class="list_name">'+(r.friends[i]['first_name']+' '+r.friends[i]['last_name'])+'</div></div></div></a></div>';
		}
		ge('friends_list').innerHTML = html;
		hide('openapi_login_wrap');
		show('openapi_block');
		show('openapi_wrap');
		//getComments();
	}else
	{
	}
}

/****DOM****/

function ge()
{
	var ea;
		for (var i = 0; i < arguments.length; i++)
		{
			var e = arguments[i];
			if (typeof e == 'string' || typeof e == 'number')
				e = document.getElementById(e);
			if (arguments.length == 1)
				return e;
			if (!ea)
				ea = new Array();
			ea.push(e);
		}
	return ea;
}

function vkNow() { return +new Date; }

function show(elem) {
  if (arguments.length > 1) {
    for (var i = 0; i < arguments.length; i++) {
      show(arguments[i]);
    }
    return;
  }
  elem = ge(elem);
  if (!elem) return;
  var old = data(elem, "olddisplay");
  elem.style.display = old || "";

  if (getStyle(elem, 'display') == "none" ) {
    if (elem.tagName.toLowerCase() == 'tr' && !browser.msie) {
      elem.style.display = 'table-row';
    } else if (elem.tagName.toLowerCase() == 'table' && !browser.msie) {
      elem.style.display = 'table';
    } else {
      elem.style.display = data(elem, "olddisplay", "block");
    }
  }
}

function hide(elem){
  if (arguments.length > 1) {
    for (var i = 0; i < arguments.length; i++) {
      hide(arguments[i]);
    }
    return;
  }
  elem = ge(elem);
  if (!elem) return;
  if (getStyle(elem, 'display') != "none")
    data(elem, "olddisplay", elem.style.display);
  elem.style.display = "none";
}

var expand = 'VK' + vkNow(), vk_uuid = 0, vk_cache = {};

// Get or set element data
function data(elem, name, data) {
  if (!elem) return false;
  var id = elem[ expand ], undefined;
  if ( !id )
    id = elem[ expand ] = ++vk_uuid;

  if (name && !vk_cache[id])
    vk_cache[id] = {};

  if (data !== undefined)
    vk_cache[id][name] = data;

  return name ?
    vk_cache[id][name] :
    id;
}

function removeData(elem, name) {
  if (!elem) return false;
  var id = elem[expand];
  if (name) {
    if (vk_cache[id]) {
      delete vk_cache[id][name];
      name = "";
      for (name in vk_cache[id])
        break;

      if (!name)
        removeData(elem);
    }
  } else {
    try {
      delete elem[expand];
    } catch(e){ // fix for IE
      if (elem.removeAttribute)
        elem.removeAttribute(expand);
    }
    delete vk_cache[id];
  }
}

/*******CSS*****/

function hasClass(obj, name)
{
	obj=ge(obj);
	return obj && (new RegExp('(\\s|^)' + name + '(\\s|$)')).test(obj.className);
}

function addClass(obj, name)
{
	obj=ge(obj);
	if (obj && !hasClass(obj, name)) obj.className = trim(obj.className + ' ' + name);
}

function removeClass(obj, name)
{
	obj=ge(obj);
	if (obj && hasClass(obj, name)) obj.className = trim(obj.className.replace((new RegExp('(\\s+|^)' + name + '(\\s+|$)')), ' '));
}